package com.anhry.app.safety.manage.enterprise.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.anhry.app.safety.manage.enterprise.bean.TbCorpInfo;
import com.anhry.app.safety.manage.enterprise.bean.TbCorpPerson;
import com.anhry.app.safety.manage.enterprise.bean.TbCorpType;
import com.anhry.app.safety.manage.enterprise.service.EnterpriseService;
import com.anhry.app.safety.manage.system.bean.TbUser;
import com.anhry.app.safety.manage.system.bean.TbUserDept;
import com.anhry.app.safety.util.Loggers;
import com.anhry.app.safety.util.dao.UtilDao;

/**
 * @date:Jun 19, 2012
 * @remark:
 * @author:dongbin
 */
@Service
@SuppressWarnings("unchecked")
public class EnterpriseServiceImpl implements EnterpriseService{
	
	@Resource
	private UtilDao utilDao;

	public UtilDao getUtilDao() {
		return utilDao;
	}

	public void setUtilDao(UtilDao utilDao) {
		this.utilDao = utilDao;
	}

	public List getCorpList(String hql, String orderBy, int start, int count)throws Exception {
		StringBuffer hqls = new StringBuffer();
		if(count == 0){
			hqls.append("select count(*) from TbCorpInfoView t ").append(hql);
		}else{
			hqls.append("from TbCorpInfoView t ").append(hql).append(orderBy);
		}
		return (List)this.utilDao.getHqlQuery(hqls.toString(), start, count);
	}

	public List getSdList(List deptList, TbUser user) throws Exception {
		try {
			List list = new ArrayList();
			String grade = user.getTbUserGrade().getId().toString();//用户级别
			if("4".equals(grade) || "6".equals(grade) || "7".equals(grade) || "8".equals(grade)){//县区安委会、县区科室、区行业、属地
				for(int i=0;i<deptList.size();i++){
					TbUserDept dept = (TbUserDept)deptList.get(i);
					if(user.getTbUserUnit().getId().toString().equals(dept.getId().toString())){
						list.add(dept);
						break;
					}
				}
				return list;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return deptList;
	}

	public TbCorpInfo getCorpByCorpName(String corpId, String corpName)throws Exception {
		StringBuilder hql = new StringBuilder("from TbCorpInfo c where c.isDele=0 and c.corpName = '"+corpName+"'");
		if(null!=corpId && corpId.length()>0){
			hql.append(" and c.id !="+corpId+"");
		}
		List list = (List)this.utilDao.getHqlQuery(hql.toString());
		if(list != null && list.size() > 0){
			return (TbCorpInfo) list.get(0);
		}
		return null;
	}

	public String getGsCode(String corpId, String gsCode) throws Exception {
		StringBuilder hql = new StringBuilder("select count(*) from TbCorpInfo c where c.isDele=0 and c.corpGsCode = '"+gsCode+"'");
		if(null!=corpId && corpId.length()>0){
			hql.append(" and c.id !="+corpId+"");
		}
		return String.valueOf(this.utilDao.getHqlQuerySize(hql.toString()));
	}

	public String getZzCode(String corpId, String zzCode) throws Exception {
		StringBuilder hql = new StringBuilder("select count(*) from TbCorpInfo c where c.isDele=0 and c.corpOrgCode = '"+zzCode+"'");
		if(null!=corpId && corpId.length()>0){
			hql.append(" and c.id !="+corpId+"");
		}
		return String.valueOf(this.utilDao.getHqlQuerySize(hql.toString()));
	}

	public TbCorpInfo getCorpById(String corpId) throws Exception {
		StringBuilder hql = new StringBuilder("from TbCorpInfo c left join fetch c.tbCorpType left join fetch c.tbCorpTypeMin" +
				" left join fetch c.tbCounty left join fetch c.tbStreet left join fetch c.tbVillage where 1=1 and c.id="+corpId);
		List list = (List)this.utilDao.getHqlQuery(hql.toString());
		if(list != null && list.size() > 0){
			return (TbCorpInfo) list.get(0);
		}
		return null;
	}

	public List getChildTypeList(TbCorpType type) throws Exception {
		List alllist = (List)this.utilDao.getHqlQuery("from TbCorpType t where 1=1 and t.isDele=0 order by t.id");
		List<TbCorpType> tempList = new ArrayList<TbCorpType>();
		List list = this.getAllChildTypes(type, alllist, tempList);
		return list;
	}
	
	/**
	 * 查找某一节点下所有子菜单
	 */
	public List<TbCorpType> getAllChildTypes(TbCorpType dept,List<TbCorpType> allList,List<TbCorpType> tempList)throws Exception {
		try {
			List<TbCorpType> list = null;
			if(dept != null && allList != null && allList.size() > 0){
				list = this.findChildTypes(dept, allList);
				if(list != null && list.size() > 0){
					for(TbCorpType fun1 : list){
						tempList.add(fun1);
						this.getAllChildTypes(fun1, allList, tempList);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("查找某企业类型下所有子类型: List<TbCorpType> getAllChildTypes(TbCorpType dept,List<TbCorpType> allList,List<TbCorpType> tempList):"+e.getMessage());
		}
		return tempList;
	}
	
	 /**
     * 查找某节点下所有的类型
     * @param dept
     * @param allList所有类型
     * @return
     * @throws Exception
     */
	public List<TbCorpType> findChildTypes(TbCorpType dept,List<TbCorpType> allList)throws Exception{
		List<TbCorpType> childNods = new ArrayList<TbCorpType>();
		try {
			if(allList != null && allList.size() > 0){
				for(TbCorpType dept1 : allList){
					if(null!=dept1.getTypeParentId() && dept.getId().toString().equals(dept1.getTypeParentId().toString())){
						childNods.add(dept1);
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			Loggers.error("查找某企业类型下所有子类型:List<TbCorpType> findChildTypes(TbCorpType dept,List<TbCorpType> allList):"+e.getMessage());
		}
		return childNods;
	}

	public TbCorpPerson getCorpPer(String corpId, String perType) throws Exception {
		StringBuilder hql = new StringBuilder("from TbCorpPerson c where 1=1 and c.corpId="+corpId+" and c.perType="+perType);
		List list = (List)this.utilDao.getHqlQuery(hql.toString());
		if(list != null && list.size() > 0){
			return (TbCorpPerson) list.get(0);
		}
		return null;
	}

	public List getTypeList(TbUser user) throws Exception {
		try {
			String hql = "";
			String grade = user.getTbUserGrade().getId().toString();//用户级别
			TbUserDept unit = null;
			TbUserDept dept = null;
			if(null!=user.getTbUserUnit()){
				unit = user.getTbUserUnit();
			}
			if(null!=user.getTbUserDept()){
				dept = user.getTbUserDept();
			}
			if("3".equals(grade) && "3".equals(dept.getDeptClass())){//市科室的行业科室
				hql = "from TbCorpType t where 1=1 and t.id in(select distinct(x.typeParentId) from TbCorpType x where x.unitId="+dept.getId()+" and x.isDele=0)";
			}else if("4".equals(grade) && "3".equals(dept.getDeptClass())){//区科室
				hql = "from TbCorpType t where 1=1 and t.id in(select distinct(x.typeParentId) from TbCorpType x where x.typeQudeptIds like '%"+dept.getId()+"%' and x.isDele=0)";
			}else if("5".equals(grade) && "1".equals(unit.getDeptClass())){//市行业
				hql = "from TbCorpType t where 1=1 and t.id in(select distinct(x.typeParentId) from TbCorpType x where x.unitId="+unit.getId()+" and x.isDele=0)";
			}else if("6".equals(grade) && "1".equals(dept.getDeptClass())){//区行业
				hql = "from TbCorpType t where 1=1 and t.id in(select distinct(x.typeParentId) from TbCorpType x where x.unitId="+dept.getDeptSj()+" and x.isDele=0)";
			}else{
				hql = "from TbCorpType t where 1=1 and t.typeParentId=0";
			}
			hql += " and t.isDele=0  order by t.id";
			List list = (List)this.utilDao.getHqlQuery(hql);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}


}
